<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/20
 * Time: 11:50
 */

namespace app\admin\controller\goods;


use app\admin\controller\BaseController;
use app\admin\controller\Upload;
use app\admin\model\Goods;
use app\admin\model\User;
use app\admin\model\Category;
use think\Session;
use think\App;
use think\Controller;
use think\Db;

//出售中
class Index extends BaseController
{
    public function page(){
        $goods=new Goods();
        $items=$goods->with(["user","category"])
            ->where("stock",">",0)
            ->where("is_del",0)
            ->select();
        //dump($page);die();
        $this->assign("title","出售中");
        $this->assign("items",$items);
        return $this->fetch("index");
    }

    //添加商品
    public function add(){
        return $this->fetch();
    }

    //快速添加商品
    public function create(){
        return $this->fetch();
    }

    //导入商品
    public function taobao(){
        return $this->fetch();
    }

    //添加，编辑商品
    public function edit(){
        $goods=new Goods();
        $user=new User();
        $category=new Category();
        if($this->request->isGet()){
            $cateItem=$category::all()->visible(["id","name"]);
            $userItem=$user->select()->visible(["uid","rname"]);
            if($this->request->has("id")){//编辑
                $id=$this->request->get("id");
                $item=$goods->find([
                    "id"    =>$id
                ]);
                //dump($item);die();
                $this->assign("item",$item);
            }else{//添加

            }
            $this->assign("cateItem",$cateItem);
            $this->assign("userItem",$userItem);
            return $this->fetch("goods/index/edit");
        }else{
            $data=$this->request->post();
            if(array_key_exists("file",$data)){
                unset($data["file"]);
            }

            if(!empty($data["id"])){//更新数据
                $goods::update($data);
                return json([
                    "code"      =>200,
                    "msg"       =>"update success"
                ]);
            }else{//插入数据
                unset($data["id"]);
                $goods->insert($data);
                return json([
                    "code"      =>200,
                    "msg"       =>"insert success"
                ]);
            }

        }
    }

    //删除商品
    public function del(){
        $ids=[];
        $id=$this->request->param("id");
        $goods=new Goods();
        if(!is_array($id)){
            $ids[]=$id;
        }else{
            $ids=$id;
        }
        //dump($id);die();
        foreach ($ids as $key =>$value){
            $res=$goods::update([
                "id"     =>$id,
                "is_del" =>1
            ]);
        }
        if($res){
            return json([
                "code"      =>200,
                "msg"       =>"success"
            ]);
        }
        //$goods::get($id)->delete();
    }

    //上下架
    public function pushOrPull(){
        $data=$this->request->param();
        $goods=new Goods();
        $res=$goods::update($data);
        if($res){
            return json([
                "code"          =>200,
                "msg"           =>"success"
            ]);
        }else{
            return json([
                "code"      =>3000,
                "errmsg"        =>"更新失败"
            ]);
        }
    }

    //添加分类
    public function category(){

        return $this->fetch();
    }
}